package com.general.framework.example.sign;

import sun.security.provider.MD5;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

public class SignatureUtil {

    // 生成签名
    public static String createSign(SortedMap<String, String> parameters, String key) throws Exception {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        // sb.append("&key=8zhd73s8djdfbxh3h37dn1js");
        sb.append("key=" + key);
        System.out.println(sb.toString());
        String sign = MD5Util.MD5Encode(sb.toString(),"UTF-8").toUpperCase();
        return sign;
    }

}